# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def current_user
    current_users
  end
  def panel_name
    @panel_name
  end
  def plain_text(text,replacement=" ")
    text.gsub(/<[^>]*>/){|html| replacement}
  end
  def summary(text,length=10)
    trc_utf8(plain_text(text),length)
  end
  def trc_utf8(text, length = 10, t_string = "...")
    l=0
    char_array=text.unpack("U*")
    char_array.each_with_index do |c,i|
    l = l+ (c<127 ? 0.5 : 1)
    if l>=length
      return char_array[0..i].pack("U*")+(i<char_array.length-1 ? t_string : "")
    end
    end
    return text
  end
  def auto_table(entities,cols_max=4,&block)
    ret="<tr>"
    nflag=0
    entities.each do |entity|
      if block_given?
        content=capture(entity,&block)
      else
        content=entity.to_s
      end
      ret<<"<td width='#{100/cols_max}%'>"<<content<<"</td>"
      nflag=nflag+1
      if nflag>=cols_max
        ret<<"</tr><tr><td colspan='#{cols_max}'>&nbsp;</td></tr><tr>"
        nflag=0
      end
    end
    if block_given?
      concat ret,block.binding
    else
      ret<<"</tr>"
    end
  end
end
